package com.neusoft.utils;

import com.neusoft.model.Emp;

/**
 * @Author 王柯淋
 * @Date 2025/1/9 14:11
 * @DESCRIBER
 */
public class EmpUtil {
    private static ThreadLocal<Emp> empThreadLocal = new ThreadLocal<Emp>();
    //从当前线程变量中获取用户信息
    public static Emp getLoginEmp() {
        System.out.println(Thread.currentThread().getId());
        Emp emp = empThreadLocal.get();
        return emp;
    }

    /**
     * 获取当前登录用户的ID
     * 未登录返回null
     *
     * @return
     */
    public static Integer getLoginEmpId() {
        Emp emp = empThreadLocal.get();
        System.out.println(emp);
        if (emp != null && emp.getId() != null) {
            Integer empID = Integer.valueOf(emp.getId());
            return empID;
        }
        return null;
    }

    //为当前的线程变量赋值上用户信息
    public static void setLoginEmp(Emp emp) {
        System.out.println(Thread.currentThread().getId());
        empThreadLocal.set(emp);
    }

    //清除线程变量
    public static void removeEmp() {
        System.out.println(Thread.currentThread().getId());
        empThreadLocal.remove();
    }
}
